jsx3.Class.defineClass(
        "tibco.admin.monitormanage.uicomponents.table.interfaces.Interfaces", //the full name of the class to create
        jsx3.gui.Block, //  extends
        null, // implements
        function (plugin) {
        	plugin.wsEndpointURL = tibco.admin.monitormanage.framework.mainPage.MainPage.wsEndpointURL;
        	
        	plugin.interfacesTableCacheId = "SPM_INTERFACES_TABLE_CACHE";
        	
        	plugin.prototype.serviceId;
        	plugin.prototype.nameSuffix = "";
        	plugin.prototype.serviceInstanceId;
        	plugin.prototype.isInstanceMode;
        	plugin.prototype.toggledRowIds;

            plugin.prototype.init = function() {
                this.jsxsuper();
            }
            
            plugin.prototype.getNamespace = function() {
               return "tibco.admin";
            }
            
            plugin.prototype.setUpComponent = function(nameSuffix, isInstanceMode,serviceId) {
				this.nameSuffix = nameSuffix;
                this.isInstanceMode = isInstanceMode;
            	this.toggledRowIds = new jsx3.util.List();
            	var screenId;
				
				var interfacesTable = this.getDescendantOfName("interfacesTable");
				interfacesTable.setName(interfacesTable.getName() + nameSuffix);
				
            	if(isInstanceMode)
            	{
                    screenId = tibco.admin.monitormanage.uicomponents.page.serviceView.ServiceView.serviceInstanceScreenId;
                    this.serviceInstanceId=serviceId;
                   	tibco.admin.monitormanage.general.PageBusUtils.subscribe(tibco.admin.monitormanage.uicomponents.page.serviceView.tabContent.operations.Operations.getServiceInstanceIdSubject, this.handleGetServiceId, this);                    
                }
            	else
            	{
                    screenId = tibco.admin.monitormanage.uicomponents.page.serviceView.ServiceView.serviceScreenId;
                     this.serviceId=serviceId; 
                   	tibco.admin.monitormanage.general.PageBusUtils.subscribe(tibco.admin.monitormanage.uicomponents.page.serviceView.tabContent.deployment.Deployment.getServiceIdSubject, this.handleGetServiceId, this);
                }
            	tibco.admin.monitormanage.general.PollingHandler.register(tibco.admin.monitormanage.general.PollingHandler.GENERAL_POLLING_HANDLER_ID, this, this.updateComponent, screenId);
            	tibco.admin.monitormanage.general.RefreshHandler.register(this, this.updateComponent, screenId);
            	tibco.admin.monitormanage.general.PageBusUtils.subscribe(tibco.admin.monitormanage.uicomponents.dropdown.timeframe.Timeframe.selectedTimeframeSubject, this.timeframeSelected, this);
				this.callGetInterfacesAndOperations();
                  this.getDescendantOfName("interfaceHelpImg").setEvent("this.getAncestorOfName('interfacesBlk').interfaceHelpImgClicked(objEVENT)", "jsxexecute");


            }
		   plugin.prototype.interfaceHelpImgClicked =function (objEVENT){
               if(this.isInstanceMode)
             this.getDescendantOfName("interfaceHelpImg").openHelp("MonitorManage.Service.Instance.InterfacesOperations  ","tibco.admin",objEVENT);
               else
             this.getDescendantOfName("interfaceHelpImg").openHelp("MonitorManage.Service.Integrated.InterfaceOperations   ","tibco.admin",objEVENT);
           }

            plugin.prototype.updateComponent = function() {
				this.callGetInterfacesAndOperations();
			}
			
			plugin.prototype.handleGetServiceId = function(subject, message, tableObj) {
			   tableObj.toggledRowIds.clear();
				if(tableObj.isInstanceMode)
				{ tableObj.serviceInstanceId = message.serviceInstanceId; }
				else
				{ tableObj.serviceId = message.serviceId; }
                tableObj.callGetInterfacesAndOperations();
			}
			
			plugin.prototype.timeframeSelected = function(subject, message, tableObj) {
            	var timeframeId = message.selectedTimeframe;
            	tableObj.callGetInterfacesAndOperations(timeframeId);
            }
            
            plugin.prototype.rowToggled = function(recordId) {
        		if(this.toggledRowIds.contains(recordId))
        		{ this.toggledRowIds.remove(recordId); }
        		else
        		{ this.toggledRowIds.add(recordId); }
        	}
            
            //call this method to begin the service call (eg.service.callgetInterfacesAndOperations();)
    		plugin.prototype.callGetInterfacesAndOperations = function(timeframeId) {
    			var dataPath = "tibco/admin/monitormanage/uicomponents/table/interfaces/data/interfacesData.xml";
    			var rulesPath = "tibco/admin/monitormanage/uicomponents/table/interfaces/rules/interfacesRules.xml";
    			if (tibco.admin.Main.getServer().getEnv("MODE")) {
                    // mode is live
                    var objService = tibco.admin.Main.loadService(this.getNamespace(), rulesPath, "getInterfacesAndOperations");
                    objService.setEndpointURL(plugin.wsEndpointURL);
		    		// this setInboundURL should not be required...only here because of a current AMX framework bug
		    		objService.setInboundURL(dataPath);
                }
                else {
                    var objService = new jsx3.net.Service(tibco.admin.Main.resolveURI(this.getNamespace(), rulesPath), "getInterfacesAndOperations", null, null);
                    // mode is static
                    objService.setInboundURL(tibco.admin.Main.resolveURI(this.getNamespace(), dataPath));
                }
                objService.interfacesObj = this;
                if(this.isInstanceMode)
                {
                	// TODO: pass the node id
                	objService.serviceId = this.serviceInstanceId;
                }
                else
                { objService.serviceId = this.serviceId; }
                if(!timeframeId)
				{ timeframeId = tibco.admin.monitormanage.uicomponents.dropdown.timeframe.Timeframe.getCurrentTimeframeId(); }
				objService.timeframeId = timeframeId;
                
                // initialize the list of metrics we wish to retrieve data for
				var metricsList = new jsx3.util.List();
				metricsList.add("faults");
				metricsList.add("successes");
				metricsList.add("throughput");
				metricsList.add("minResponseTime");
				metricsList.add("maxResponseTime");
				metricsList.add("avgResponseTime");
				metricsList.add("minMessageSize");
				metricsList.add("maxMessageSize");
				metricsList.add("avgMessageSize");
				objService.metricsList = metricsList;

      			//subscribe and call
      			objService.subscribe(jsx3.net.Service.ON_SUCCESS, this.ongetInterfacesAndOperationsSuccess);
      			objService.subscribe(jsx3.net.Service.ON_ERROR, this.ongetInterfacesAndOperationsError);
      			objService.subscribe(jsx3.net.Service.ON_INVALID, this.ongetInterfacesAndOperationsInvalid);
      			objService.doCall();
    		};

    		plugin.prototype.ongetInterfacesAndOperationsSuccess = function(objEvent) {
      			//var responseXML = objEvent.target.getInboundDocument();
      			//jsx3.log("Success","The service call was successful.");
      			
      			var cacheDoc = tibco.admin.Main.getServer().Cache.getDocument(plugin.interfacesTableCacheId);
      			var interfaceRecords = cacheDoc.getChildNodes();
      			for(i = 0; i < interfaceRecords.size(); i++)
      			{
      				var interfaceRecord = interfaceRecords.get(i);
      				var operationRecords = interfaceRecord.getChildNodes();
      				for(j = 0; j < operationRecords.size(); j++)
      				{
      					var operationRecord = operationRecords.get(j);
      					var faults = operationRecord.getAttribute("faults");
      					var successes = operationRecord.getAttribute("successes");
      					var faultSuccessRatio = faults + "/" + successes;
      					var faultSuccessRatioValue = faults/successes;
      					operationRecord.setAttribute("faultSuccessRatioValue",faultSuccessRatioValue);
                          operationRecord.setAttribute("faultSuccessRatio",faultSuccessRatio);

                          var hits = (faults - 0) + (successes - 0);
      					operationRecord.setAttribute("hits", hits);
      				}
      			}
      			
      			var interfacesObj = objEvent.target.interfacesObj;
      			var interfacesTable = interfacesObj.getDescendantOfName("interfacesTable" + interfacesObj.nameSuffix);
      			
      			// now make sure we persist the toggled rows
      			for(j = 0; j < interfacesObj.toggledRowIds.size(); j++)
      			{
      				var rowId = interfacesObj.toggledRowIds.get(j);
      				interfacesTable.toggleItem(rowId, true);
      			}

      			interfacesTable.repaintData();
    		};

    		plugin.prototype.ongetInterfacesAndOperationsError = function(objEvent) {
      			var myStatus = objEvent.target.getRequest().getStatus();
      			jsx3.log("Error","The service call failed. The HTTP Status code is: " + myStatus);
    		};

    		plugin.prototype.ongetInterfacesAndOperationsInvalid = function(objEvent) {
      			jsx3.log("Invalid","The following message node just failed validation:\n\n" + objEvent.message);
    		};

        });